﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eStock.hControl;
using HCommon;

namespace eStock
{
    public partial class frmItems_Add : hForm
    {
        public frmItems_Add()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Error == true) { btnSave.Error = false; return; }
            SaveMaster();

        }

        private void SaveMaster()
        {
            try
            {
                int rs = 0;
                if (this.FormMode == Mode.Insert)
                {
                    rs = tblItem.Insert(txtItemCd.Text, txtItemName.Text, cboUnitCd.SelectedValue.ToString(), cboOrigin.SelectedValue.ToString());
                }
                else
                {
                    rs = tblItem.Update(txtItemCd.Text, txtItemName.Text, cboUnitCd.SelectedValue.ToString(), cboOrigin.SelectedValue.ToString());
                }

                SaveGrid();

                Dialog.ShowMess(rs);
                this.UpdateKey = txtItemCd.Text;
                this.FormMode = Mode.Update;
                LoadMaster();
                LoadGrid();
            }
            catch (Exception ex)
            {
                Common.GhiLog("SaveMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void LoadMaster()
        {
            try
            {
                if (this.FormMode == Mode.Insert)
                {
                    txtItemCd.ReadOnly = false;
                    Common.ClearControl(grbItem);
                    return;
                }

                txtItemCd.ReadOnly = true;
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("SELECT * FROM tblItem with(nolock) where ItemCd = N'" + this.UpdateKey + "'");
                if (dt.Rows.Count == 0)
                {
                    Dialog.ShowMess(Dialog.mType.err, "Mã vật tư này đã bị xóa!");
                    this.Close();
                    return;

                }
                txtItemCd.Text = dt.Rows[0]["ItemCd"].ToString();
                txtItemName.Text = dt.Rows[0]["ItemName"].ToString();
                cboUnitCd.SelectedValue = dt.Rows[0]["UnitCd"].ToString();
                cboOrigin.SelectedValue = dt.Rows[0]["Origin"].ToString();
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void SaveGrid()
        {
            int rs = 0;
            foreach (DataGridViewRow row in grdPrice.Rows)
            {
                try
                {
                    if (row.IsNewRow == true)
                    {
                        continue;
                    }
                    if (row.Cells["ID"].Value == null || row.Cells["ID"].Value.ToString().Trim().Length == 0)
                    {
                        rs = tblItem_Price.Insert(txtItemCd.Text, Common.VNDateToSQL(row.Cells["FromDate"].Value.ToString()), row.Cells["Price"].Value.ToString());
                    }
                    else
                    {
                        rs = tblItem_Price.Update(row.Cells["ID"].Value.ToString(), txtItemCd.Text, Common.VNDateToSQL(row.Cells["FromDate"].Value.ToString()), row.Cells["Price"].Value.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Common.GhiLog("SaveGrid", ex);
                }
            }
        }

        private void LoadGrid()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("Exec spItem_Price_Q '" + txtItemCd.Text + "'");
                if (dt != null)
                {
                    grdPrice.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadGrid", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void frmCustomer_Add_Load(object sender, EventArgs e)
        {
            LoadMaster();
            LoadGrid();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.FormMode = Mode.Insert;
            LoadMaster();
            LoadGrid();
        }

        private void grdPrice_hDelete(object sender, EventArgs e)
        {
            try
            {
                int delCnt = 0;
                foreach (DataGridViewRow row in grdPrice.SelectedRows)
                {
                    if (row.IsNewRow == true)
                    {
                        continue;
                    }
                    if (tblItem_Price.Delete(row.Cells["ID"].Value.ToString()) > 0)
                    {
                        grdPrice.Rows.Remove(row);
                        delCnt++;
                    }
                }
                Dialog.ShowMess(Dialog.mType.ok, "Đã xóa thành công " + delCnt + " bản ghi.");
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdPrice_hDelete", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

    }
}
